<?php

namespace App\Api\Controllers;

use App\Models\Squad;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
use Encore\Admin\Facades\Admin;

class GetSquadController extends Controller
{
    public function get_squads(Request $request)
    {
        $group_id = $request->get('q');
        $data = Squad::where('group', $group_id)->pluck(['name','id']);
        return $data;
    }

    public function get_squads_user(Request $request)
    {
        $user = $request->get('user');
        $group_id = $request->get('q');

        $num = DB::table('class_section_groups')->where('id','=',$group_id)->get(['pid']);
        $pid = 0;
        foreach($num as $v){
            $pid = $v->pid;
        }
        $section = DB::table('class_section_groups')->where('id','=',$pid)->whereRaw('find_in_set('.$user.',`charge`)')->count();
        if($section > 0){
            //是班段负责人，返回该分组下所有班级
            $data = DB::table('squads')->where('group','=',$group_id)->get(['id','name']);
        }else{
            $group = DB::table('class_section_groups')->where('id','=',$group_id)->whereRaw('find_in_set('.$user.',`charge`)')->count();
            if($group>0){
                //是该分组负责人，直接返回该分组下所有班级
                $data = DB::table('squads')->where('group','=',$group_id)->get(['id','name']);
            }else{
                //不是该分组负责人，返回带的班级
                $data = DB::table('squads')->where('group','=',$group_id)->where('class_teacher','=',$user)->get(['id','name']);
            }
        }

        $arr = array();
        $arr[] = array('id'=>0,'text'=>'请选择班级');
        foreach($data as $v){
            $arr[] =  array('id'=>$v->id,'text'=>$v->name);
        }
        return $arr;
    }
}
